// 插入新行相关
var runButtonAddLine = document.querySelector("#run-button-addline");
runButtonAddLine.addEventListener("click", addLine);

function addLine() {
  // 储存输入
  var serialNumber = parseInt(
    document.getElementsByName("hidden-data")[0].value
  );
  var timeLength = new Array();
  var ruleIsNumber = /^(\-|\+)?\d+$/;

  for (i = 1; i <= serialNumber; i++) {
    var hour = parseInt(document.getElementById("h" + i).value);
    var miunte = parseInt(document.getElementById("m" + i).value);
    var second = parseInt(document.getElementById("s" + i).value);

    // 未填写项及非数字项，自动设置为0
    timeLength[i - 1] = [hour, miunte, second];
    for (m = 0; m < timeLength[i - 1].length; m++) {
      if (!ruleIsNumber.test(timeLength[i - 1][m])) {
        timeLength[i - 1][m] = 0;
      }
    }
  }

  // 更新序号
  serialNumber += 1;
  document
    .getElementsByName("hidden-data")[0]
    .setAttribute("value", serialNumber);

  // 插入新行
  var buttonContainer = document.getElementById("data-entry-table");
  buttonContainer.innerHTML += [
    '<div class="row data-entry" id="data-entry-' + serialNumber + '">',
    '<div class="entry-number">' + serialNumber + ':</div>',
    '<input type="text" id="h' +
      serialNumber +
      '" placeholder="0" class="timeInput">',
    '<div class="time-input-text">时</div>',
    '<input type="text" id="m' +
      serialNumber +
      '" placeholder="0" class="timeInput">',
    '<div class="time-input-text">分</div>',
    '<input type="text" id="s' +
      serialNumber +
      '" placeholder="0" class="timeInput">',
    '<div class="time-input-text">秒</div>',
    '<button id="run-button-addline">+</button>',
    "</div>",
  ].join("");
  buttonContainer.parentElement.appendChild(buttonContainer);

  // 还原输入
  for (k = 1; k <= serialNumber - 1; k++) {
    document.getElementById("h" + k).value = timeLength[k - 1][0];
    document.getElementById("m" + k).value = timeLength[k - 1][1];
    document.getElementById("s" + k).value = timeLength[k - 1][2];
  }

  // 删除多余按钮
  var deletElement = document.getElementById("run-button-addline");
  deletElement.parentNode.removeChild(deletElement);

  // 更新按钮监听
  runButtonAddLine = document.querySelector("#run-button-addline");
  runButtonAddLine.addEventListener("click", addLine);
}

// 计算时长
var runButtonCalculate = document.querySelector("#run-button-calculate");
runButtonCalculate.addEventListener("click", calculateResult);

function calculateResult() {
  var serialNumber = parseInt(
    document.getElementsByName("hidden-data")[0].value
  );
  var timeLength = new Array();
  var ruleIsNumber = /^(\-|\+)?\d+$/;

  for (i = 1; i <= serialNumber; i++) {
    var hour = parseInt(document.getElementById("h" + i).value);
    var miunte = parseInt(document.getElementById("m" + i).value);
    var second = parseInt(document.getElementById("s" + i).value);

    // 未填写项及非数字项，自动设置为0
    timeLength[i - 1] = [hour, miunte, second];
    for (m = 0; m < timeLength[i - 1].length; m++) {
      if (!ruleIsNumber.test(timeLength[i - 1][m])) {
        timeLength[i - 1][m] = 0;
      }
    }
  }

  // 调试检查用
  for (j = 1; j <= serialNumber; j++) {
    console.log(
      "条目" +
        j +
        ": " +
        timeLength[j - 1][0] +
        ", " +
        timeLength[j - 1][1] +
        ", " +
        timeLength[j - 1][2]
    );
  }

  // 分类求和，计算时长
  var sumHour = 0;
  var sumMinute = 0;
  var sumSecond = 0;

  for (k = 1; k <= serialNumber; k++) {
    sumHour += timeLength[k - 1][0];
    sumMinute += timeLength[k - 1][1];
    sumSecond += timeLength[k - 1][2];
  }

  var sumInSecond = sumHour * 60 * 60 + sumMinute * 60 + sumSecond;
  console.log(sumInSecond + "秒");

  var isNegative = false;
  if (sumInSecond < 0) {
    isNegative = true;
    sumInSecond = -sumInSecond;
  }

  sumHour = Math.floor(sumInSecond / (60 * 60));
  sumMinute = Math.floor((sumInSecond - sumHour * 60 * 60) / 60);
  sumSecond = Math.floor(sumInSecond - sumHour * 60 * 60 - sumMinute * 60);

  // 值为负时添加负号
  var textprefix = "";
  if (isNegative) {
    textprefix = "-";
  }

  // 显示结果
  var resultContainer = document.getElementById("result-container");
  resultContainer.innerHTML = [
    '<div class="row text-result" id="text-result">' + textprefix + sumHour + "小时" + sumMinute + "分钟" + sumSecond + "秒" + '</div>',
  ].join("");
  resultContainer.parentElement.appendChild(resultContainer);
}
